/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package com.niannian.activity;

import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.MyTextWatcher;
import com.niannian.web.nnSyncApi;
import com.niannian.BaseActivity;
import com.niannian.R;

/**
 * 修改密码
 * 
 * @author shengl
 * 
 */
public class ChangePasswordActivity extends BaseActivity {
	private EditText reg_change_password, reg_change_password_new, reg_change_password_repeat;
	private Button btn_change_password_submit;
	String password = "", password1 = "", password2 = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		initView();

		setLister();
		initTopView();

	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("修改密码");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});

	}

	private MyTextWatcher textWatcher = new MyTextWatcher() {
		@Override
		public void onFocusChange(View view, boolean hasFocus) {
		};

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			password = reg_change_password.getText().toString().trim();
			password1 = reg_change_password_new.getText().toString().trim();
			password2 = reg_change_password_repeat.getText().toString().trim();

			if (!Common.empty(password) && !Common.empty(password1) && !Common.empty(password2)) {
			//	btn_change_password_submit.setEnabled(true);
			} else {
				//btn_change_password_submit.setEnabled(false);
			}
		}
	};

	private void initView() {
		reg_change_password = (EditText) findViewById(R.id.reg_change_password);
		reg_change_password_new = (EditText) findViewById(R.id.reg_change_password_new);
		reg_change_password_repeat = (EditText) findViewById(R.id.reg_change_password_repeat);
		btn_change_password_submit = (Button) findViewById(R.id.btn_change_password_submit);

		reg_change_password.addTextChangedListener(textWatcher);
		reg_change_password_new.addTextChangedListener(textWatcher);
		reg_change_password_repeat.addTextChangedListener(textWatcher);
	}

	private void setLister() {

		btn_change_password_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				password = reg_change_password.getText().toString().trim();
				password1 = reg_change_password_new.getText().toString().trim();
				password2 = reg_change_password_repeat.getText().toString().trim();

				if (!password1.equals(password2)) {
					Common.tip(ChangePasswordActivity.this, "2次输入的密码不一样");
					return;
				}
				switch (Common.checkPassword(password2)) {
				case 1:
					Common.tip(ChangePasswordActivity.this, "密码应该是数字与字符的组合");
					return;
				case 2:
					Common.tip(ChangePasswordActivity.this, "密码太短，请至少输入6个字符");
					return;
				case 3:
					Common.tip(ChangePasswordActivity.this, "密码太长,请至多输入16个字符");
					return;
				}

				new PasswordChange().init(ChangePasswordActivity.this, null, true, "设置密码中...").execute();

			}
		});

	}

	protected class PasswordChange extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {

			Common.tip(ChangePasswordActivity.this, "修改密码失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {

			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(ChangePasswordActivity.this, "修改密码成功");
				myfinish();
			} else {
				Common.tip(ChangePasswordActivity.this, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.changePassword(password, password1);
		}
	}

	@Override
	public void onBackPressed() {
		myfinish();
	}

}
